/**
 * 
 */
package com.taobao.client.pages;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Frame;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.taobao.client.Data;
import com.taobao.client.HeadPanel;
import com.taobao.client.RPC;
import com.taobao.client.util.PopBox;

/**
 * 
 * @author hujingfei.pt
 *
 */
public class ItemRejection extends Composite {
	private static ItemRejection itemRejection;		
	private final static String RESULT_FLAG = "refundExplain";	
	public final static String HISTORYITEM = "ItemRejectionPage";	
	
	private Frame f;	
	private AsyncCallback<String> inputCallBack = new AsyncCallback<String>() {
		@Override
		public void onFailure(Throwable arg0) {
			PopBox.error("Construct URL Error", arg0.getMessage());
		}

		@Override
		public void onSuccess(String arg0) {
			f.setUrl(arg0);
			f.setSize(Data.width, Data.height);
		}
	};
	public static InputParamters input;	
	
	public static ItemRejection getItemRejection() {
		if (itemRejection == null) {
			itemRejection = new ItemRejection();
		}
		return itemRejection;
	}

	

	public static void initInputParameters(String bizOrderId, String refundId) {
		input = new InputParamters();
		input.bizOrderId = bizOrderId;
		input.refundId = refundId;
		input.url = Window.Location.getHref();		
	};

	/**
	 * Encapsulate the input parameters of rejecting items
	 * @author hujingfei.pt
	 *
	 */
	private static class InputParamters {
		/*
		 * Name				Type	Must Or Not	Description
		 * bizOrderId		Long	是			订单编号
		 * refundId			String	是			退货id
		 * url				url	           是			回调url
		 * headerUrl		url	   	是			页头url
		 * footerUrl		url		是			页脚url
		 * auctionRemark	String	否			商品备注
		 * orderRemark		String	否			订单备注
		 */
		private String bizOrderId;
		private String refundId;
		private String url;
		private String auctionRemark; 
		private String orderRemark;

		public Map<String, String> getParam() {
			// Construct the input parameters pairs of the application
			HashMap<String, String> m = new HashMap<String, String>();
			m.put("method", Data.API_CONFIRMATION_GOODS);
			m.put("bizOrderId", bizOrderId);
			m.put("refundId", refundId);
			// Buyer reject the item
			m.put("userAction", "2");
			m.put("url", url);
//			m.put("headerUrl", "http://"+Window.Location.getHost()+"/"+Data.headUrl);
//			m.put("footerUrl",  "http://"+Window.Location.getHost()+"/"+Data.bottomUrl);
			m.put("auctionRemark", auctionRemark);
			m.put("orderRemark", orderRemark);
			return m;
		}
	}

	/**
	 * Encapsulate the output parameters of rejecting items
	 * @author hujingfei.pt
	 *
	 */
	private class OutputParameters {
		
		/* 
		 * Name				Type	Must Or Not	Description
		 * bizOrderId		Long	是			订单编号
		 * logisticsCompany	int		是			物流公司
		 * billNo			String	是			运单号
		 * refundExplain	String	是			发货说明
		 * timeOut			Date	是			卖家处理超时时间
		 */
		private String bizOrderId;
		private String logisticsCompany;
		private String billNo;	
		private String refundExplain;	;
		private String timeOut;

		public OutputParameters() {
			String queryString = Window.Location.getQueryString();
			String[] split = queryString.split("&");
			for (String sp : split) {
				String[] split2 = sp.split("=");

				if (split2[0].indexOf("bizOrderId") >= 0) {
					bizOrderId = split2[1];
				}
				if (split2[0].indexOf("logisticsCompany") >= 0) {
					logisticsCompany = split2[1];
				}
				if (split2[0].indexOf("billNo") >= 0) {
					billNo = split2[1];
				}				
				if (split2[0].indexOf("refundExplain") >= 0) {
					refundExplain = split2[1];
				}				
				if (split2[0].indexOf("timeOut") >= 0) {
					timeOut = split2[1];
				}				
			}
		}
	}

	/**
	 * 
	 */
	private ItemRejection() {

		String queryString = Window.Location.getQueryString();
		System.out.println("QueryString:\t" + queryString);
		
		if (queryString != null && queryString.indexOf(RESULT_FLAG) >= 0) {
			processOutput();
		} else {
			processInput();
		}
	}

	/**
	 * 
	 */
	private void processInput() {
		VerticalPanel content = new VerticalPanel();
		content.add(createHead("Apply ItemRejection"));

		f = new Frame();
		content.add(f);

		if (input == null) {
			content.add(new Label("Login illegal, please login again"));
		} else {
			RPC.getService().getUrl(input.getParam(), inputCallBack);
		}

		initWidget(content);
	}

	

	/**
	 * 
	 */
	private void processOutput() {
		
		/* 
		 * Name				Type	Must Or Not	Description
		 * bizOrderId		Long	是			订单编号
		 * logisticsCompany	int		是			物流公司
		 * billNo			String	是			运单号
		 * refundExplain	String	是			发货说明
		 * timeOut			Date	是			卖家处理超时时间
		 */
		
		VerticalPanel content = new VerticalPanel();
		content.add(createHead("Item Rejection Response"));
		OutputParameters o = new OutputParameters();

		// Show results
		content.add(new Label("bizOrderId:\t" + o.bizOrderId));
		content.add(new Label("Logistics Company:\t" + o.logisticsCompany));
		content.add(new Label("Bill Number:\t " + o.billNo));
		content.add(new Label("Refund Explain:\t " + o.refundExplain));
		content.add(new Label("Time out:\t " + o.timeOut));	
		
		initWidget(content);
	}

	/**
	 * 
	 * @param title
	 * @return
	 */
	private HeadPanel createHead(String title) {
		HeadPanel head = new HeadPanel();
		head.add(new Label(title));
		return head;
	}
}